import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:equatable/equatable.dart';

import '../../../../data/controllers/test_controller.dart';
import '../../../../data/remote/database_helper.dart';
import '../models/test.dart';

part 'tests_state.dart';
part 'tests_event.dart';

class TestsCubit extends Bloc<TestsEvent,TestsState> {
  TestsCubit(TestsState testsInitial) : super(testsInitial) {
    on<TestsInitialEvent>(_onInitialize);
    // for add player
    on<AddTestLoadingTapEvent>(_onAddTestLoading);
    on<AddTestFailedTapEvent>(_onAddTestFailed);
    on<AddTestSuccessTapEvent>(_onAddTestSuccess);
    // for update player
    on<UpdateTestLoadingEvent>(_onUpdateTestLoading);
    on<UpdateTestFailedEvent>(_onUpdateTestFailed);
    on<UpdateTestSuccessEvent>(_onUpdateTestSuccess);
    //for delete player
    on<DeleteTestLoadingEvent>(_onDeleteTestLoading);
    on<DeleteTestFailedEvent>(_onDeleteTestFailed);
    on<DeleteTestSuccessEvent>(_onDeleteTestSuccess);
    on<ChangeSelectMode>(_onChangeSelectMod);
    on<ChangeSelectedItem>(_onChangeSelectedItem);
    on<TabSelectAll>(_onTabSelectAll);
  }
  static TestsCubit get(context) => BlocProvider.of(context);



  TextEditingController searchBox = TextEditingController();

  List<Test> allTests = [];
  List<Test> selectedTests = [];
  List<Test> tests = [];



  bool searchFlag = false;
  bool selectedMode = false;

  _onInitialize(
      TestsInitialEvent event,
      Emitter<TestsState> emit,
      ) async {
    searchBox.clear();
    emit(TestsLoading());
    await TestController.getMyTests().catchError((e){
      emit(TestsError());
      return null;
    }).then((value)async{
      if(value != null && value.state) {
        tests = List<Test>.generate(value.data.length, (index) => Test.fromJson(value.data[index]));
        allTests = tests.where((e)=>true).toList();
        localeMyData();
        emit(TestsDone());
      }else if(value != null){
        //var res = await DatabaseHelper.getAllTests();
        //tests = List<Test>.generate(res.length, (index) => Test.fromJson(res[index]));
        //allTests = tests.where((e)=>true).toList();
        emit(TestsDone());
      }else{
        if (kDebugMode) {
          print('value is null');
        }
        emit(TestsLoading());
        //var res = await DatabaseHelper.getAllTests();
        //tests = List<Test>.generate(res.length, (index) => Test.fromJson(res[index]));
        //allTests = tests.where((e)=>true).toList();
        emit(TestsDone());
      }
    });
  }
  _onAddTestSuccess(
      AddTestSuccessTapEvent event,
      Emitter<TestsState> emit,
      ) async {
    tests.add(event.test);
    allTests.add(event.test);
    emit(TestsUpdate());
  }
  _onAddTestFailed(
      AddTestFailedTapEvent event,
      Emitter<TestsState> emit,
      ) async {
  }
  _onAddTestLoading(
      AddTestLoadingTapEvent event,
      Emitter<TestsState> emit,
      ) async {
    print("جاري اضافة لاعب");
  }

  _onUpdateTestSuccess(
      UpdateTestSuccessEvent event,
      Emitter<TestsState> emit,
      ) async {

    for(int i =0 ;i<tests.length;i++){
      /*if(tests[i].testId == event.test.testId){
        tests[i]= event.test;
      }*/

    }
    emit(TestsUpdate());
  }
  _onUpdateTestFailed(
      UpdateTestFailedEvent event,
      Emitter<TestsState> emit,
      ) async {
  }

  _onUpdateTestLoading(
      UpdateTestLoadingEvent event,
      Emitter<TestsState> emit,
      ) async {
  }
  _onDeleteTestSuccess(
      DeleteTestSuccessEvent event,
      Emitter<TestsState> emit,
      ) async {
    //tests.removeWhere((e)=>e.testId == event.testId);
    emit(TestsUpdate());
  }
  _onDeleteTestFailed(
      DeleteTestFailedEvent event,
      Emitter<TestsState> emit,
      ) async {
  }
  _onDeleteTestLoading(
      DeleteTestLoadingEvent event,
      Emitter<TestsState> emit,
      ) async {
    print("جاري تحديث لاعب");
  }
  _onChangeSelectMod(
      ChangeSelectMode event,
      Emitter<TestsState> emit,
      ) async {
    selectedMode = event.mod;
    print(event.mod);
    emit(TestsUpdate());
  }

  _onChangeSelectedItem(
      ChangeSelectedItem event,
      Emitter<TestsState> emit,
      ) async {
    if(event.value){
      selectedTests.add(event.test);
    }else{
      selectedTests.remove(event.test);
    }
  }
  _onTabSelectAll(
      TabSelectAll event,
      Emitter<TestsState> emit,
      ) async {
    if(event.value == true) {
      for (Test test in tests) {
        selectedTests.add(test);
      }
    }else{
      for (Test test in tests) {
        selectedTests.remove(test);
      }
    }
    emit(SelectAllTestsState(value: event.value));
  }
  localeMyData()async{

  }


  onSearchTap(){
    searchFlag = !searchFlag;
    emit(TestsUpdate());
  }
  onSearch(String text){
    if(text.isEmpty){
      tests = allTests.where((e)=>true).toList();
    }else {

    }
    emit(TestsUpdate());
  }

}
